public class DesignCircularQueue {
    int[] queue;
    int front; // 头指针
    int rear; // 尾指针
    int size; // 队列长度

    public DesignCircularQueue(int k) {
        // 多加 1 个空间来判断队列是否为空或为满
        queue = new int[k+1];
        this.size = k + 1;
    }

    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        queue[rear] = value;
        rear = (rear + 1) % size;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % size;
        return true;
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return queue[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = (rear == 0 ? size - 1 : rear - 1);
        return queue[index];
    }

    public boolean isEmpty() {
        // 首尾指针相遇说明为空
        return front == rear;
    }

    public boolean isFull() {
        // 尾指针下一个如果与头指针相遇, 则说明队列已满
        return (rear + 1) % size == front;
    }
}
